package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.cdn.dao.CdnNetInfoLinkDao;

@Repository
public class CdnNetInfoLinkDaoImpl implements CdnNetInfoLinkDao {

	private static final Log log = LogFactory.getLog(CdnDataDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(IMap param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(IMap param){
		String linkDomainId = CommonUtils.valueOf(param.get("linkDomainId"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		
		sql.append("select net.cdnId,net.cdnNetId,net.topDomain,net.regId,(select cdn.cdnName from t_cdn_cdndata cdn where cdn.cdnId = net.cdnId) as cdnName,timstamp");
		sql.append("  from t_cdn_cdnNetInfo net where 1 = 1");
		if(StringUtils.isNotBlank(linkDomainId)) {//域名页面下钻过来
			sql.append(getQueryConf(param));
		}else {//企业页面下钻过来
			sql.append(getQueryConf(param));
			sql.append(" group by net.cdnNetId,net.topDomain,net.regId");
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(IMap param) {
		String linkDomainId = CommonUtils.valueOf(param.get("linkDomainId"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.isNotBlank(linkDomainId)) {//域名页面下钻过来
			sql.append(" select count(1) count from t_cdn_cdnNetInfo net where 1 = 1");
			sql.append(getQueryConf(param));
		}else {//企业页面下钻过来
			sql.append("select count(1) count from(");
			sql.append(" select cdnNetId from t_cdn_cdnNetInfo net where 1 = 1");
			sql.append(getQueryConf(param));
			sql.append(" group by net.cdnNetId,net.topDomain,net.regId");
			sql.append(") t");
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	
	public StringBuffer getQueryConf(IMap param) {
		String topDomain = CommonUtils.valueOf(param.get("topDomain"));
		String cdnNetId = CommonUtils.valueOf(param.get("cdnNetId"));
		String linkCdnId = CommonUtils.valueOf(param.get("linkCdnId"));
		String linkDomainId = CommonUtils.valueOf(param.get("linkDomainId"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(cdnNetId)) {
			queryConf.append(" and net.cdnNetId like '%"+cdnNetId+"%'");
		}
		if(StringUtils.isNotBlank(topDomain)) {
			queryConf.append(" and net.topDomain like '%"+topDomain+"%'");
		}
		if(StringUtils.isNotBlank(linkCdnId)) {
			queryConf.append(" and net.cdnId='"+linkCdnId+"'");
		}
		if(StringUtils.isNotBlank(linkDomainId)) {
			queryConf.append(" and net.domainId='"+linkDomainId+"'");
		}
		return queryConf;
	}
}
